/*
 * COPYRIGHT:	2010 Christopher Smith, licenced under the GNU GPL v2; see COPYING in the top level directory
 * PROJECT:		sdedit
 * FILE:		dialogcallback.cpp
 * PURPOSE:		generic dialog callback with context mechanism
 *				
 */

class DialogCallback
{
	DIALOG_CALLBACK_PROC CallbackProc;
	PVOID CallbackContext;
	BOOLEAN SentCloseCallback;

public:
	DialogCallback(DIALOG_CALLBACK_PROC CallbackProc, PVOID CallbackContext) :
		CallbackProc(CallbackProc),
		CallbackContext(CallbackContext),
		SentCloseCallback(FALSE)
	{}

	template <class T> LRESULT SendCallback(DIALOG_NOTIFY_OPERATION Operation, T Data)
	{
		if(SentCloseCallback)
			return 0;

		LRESULT Result = CallbackProc(CallbackContext, Operation, c_cast<ULONG_PTR>(Data));

		if(Operation <= DN_CLOSE_MAX)
			SentCloseCallback = TRUE;

		return Result;
	}

	template <class T> LRESULT SendCallback(DIALOG_NOTIFY_OPERATION Operation, auto_szpod<T> Data)
	{
		if(SentCloseCallback)
			return 0;

		LRESULT Result = CallbackProc(CallbackContext, Operation, c_cast<ULONG_PTR>(Data.get()));

		if(Operation <= DN_CLOSE_MAX)
			SentCloseCallback = TRUE;

		return Result;
	}
};